package org.jeecg.modules.paper.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xkcoding.http.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.api.SynchVo;
import org.jeecg.modules.api.service.PaperService;
import org.jeecg.modules.content.entity.ConContentTemplate;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IConContentTemplateService;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.paper.entity.PaperLanding;
import org.jeecg.modules.paper.service.IPaperLandingService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: paper
 * @Author: jeecg-boot
 * @Date:   2021-07-02
 * @Version: V1.0
 */
@Slf4j
@Api(tags="paper")
@RestController
@RequestMapping("/paper/paperLanding")
public class PaperLandingController extends JeecgController<PaperLanding, IPaperLandingService> {
	@Autowired
	private IPaperLandingService paperLandingService;
	@Autowired
	private  PaperService paperService;
	@Autowired
	private IContentInfoService contentInfoService;
	@Autowired
	private IConContentTemplateService conContentTemplateService;







	 @GetMapping(value = "/sync")
	 public Result<?> add( String id) {
		 ConContentTemplate conContentTemplate = conContentTemplateService.getById(id);
			 //同步到纸媒
			 SynchVo synchVo = new SynchVo();
			 synchVo.setAuthor(conContentTemplate.getAuthorName());
			 synchVo.setContent(conContentTemplate.getContent().toString());
			 synchVo.setSubTitle(conContentTemplate.getContentSubTitle());
			 synchVo.setTitle(conContentTemplate.getContentTitle());
			 synchVo.setId(conContentTemplate.getId());
			 String result = paperService.synch(synchVo);
			 log.info(result);
			 if(result !=null ){
				 JSONObject jsonObject =JSONObject.parseObject(result);
				 if(jsonObject.get("success").toString().equals("true")){
					 conContentTemplate.setSyncStatus(1);
					 conContentTemplateService.updateById(conContentTemplate);
				 }else {
					 conContentTemplate.setSyncStatus(3);
					 conContentTemplateService.updateById(conContentTemplate);
					 return Result.error("同步失败！");
				 }
			 }else {
				 return Result.error("同步失败！");
			 }



		 return Result.ok("同步成功！");
	 }


	/**
	 * 分页列表查询
	 *
	 * @param paperLanding
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "paper-分页列表查询")
	@ApiOperation(value="paper-分页列表查询", notes="paper-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PaperLanding paperLanding,String startTime,String endTime,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PaperLanding> queryWrapper = QueryGenerator.initQueryWrapper(paperLanding, req.getParameterMap());
		Page<PaperLanding> page = new Page<PaperLanding>(pageNo, pageSize);
		if(!StringUtil.isEmpty(startTime)){
			queryWrapper.ge("PERIODNUM",startTime);

		}
		if(!StringUtil.isEmpty(endTime)){
			queryWrapper.le("PERIODNUM",endTime);

		}
		IPage<PaperLanding> pageList = paperLandingService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @param paperLanding
	 * @return
	 */
	@AutoLog(value = "paper-添加")
	@ApiOperation(value="paper-添加", notes="paper-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PaperLanding paperLanding) {
		paperLandingService.save(paperLanding);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param paperLanding
	 * @return
	 */
	@AutoLog(value = "paper-编辑")
	@ApiOperation(value="paper-编辑", notes="paper-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PaperLanding paperLanding) {
		if(paperLanding.getContentId()==null){
			paperLanding.setContent(" ");
		}
		paperLandingService.updateById(paperLanding);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "paper-通过id删除")
	@ApiOperation(value="paper-通过id删除", notes="paper-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		paperLandingService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "paper-批量删除")
	@ApiOperation(value="paper-批量删除", notes="paper-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.paperLandingService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "paper-通过id查询")
	@ApiOperation(value="paper-通过id查询", notes="paper-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PaperLanding paperLanding = paperLandingService.getById(id);
		return Result.ok(paperLanding);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param paperLanding
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, PaperLanding paperLanding) {
      return super.exportXls(request, paperLanding, PaperLanding.class, "paper");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, PaperLanding.class);
  }

}
